<?php
/*
	插件概览
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get';
if($_RQ['op']=='get'){
	if($_SESSION['os']=='windows'){
		$return['session_id']=session_id();
	}
	$_SESSION['userid']='';
	$_SESSION['merchid']='';
	$_SESSION['logout_forward']='jy_51job/login/';
	$_SESSION['forward_url']='jy_51job/login/'; 
	$return['forward_url']=base64_encode($_SESSION['forward_url']);
	$return['info']['mid']=$_SESSION['uid'];
	$return['merch_account']=pdo_get('jy_51job_merch_account',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
	set_html('title','企业登录');
	exi($return);
}
if($_RQ['op']=='post'){
	$post=get_postdata();
	if(!$post['password'] || !$post['username']){
		exi('请填写账户名和密码','error');
	}
	$tablename='jy_51job_merch_account';
	$user=pdo_get($tablename,array('uniacid'=>$_SESSION['uniacid'],'name'=>$post['username']));
	if(!$user || md5($post['password'])!=$user['password']){
		exi('用户名或密码错误','error');
	}
	if(!$user['status']){
		exi('您的账号还未启用！','error');
	}
	if($user['status']==2){
		exi('改账户无权限，不能登录！','error');
	}
	if($user['mid']){
		$user['mid']=pdo_getcolumn('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$user['mid']),'id');
	}
	if(!$user['mid'] && $_SESSION['uid']){
		$user['mid']=$_SESSION['uid'];
		pdo_update($tablename,array('mid'=>$user['mid']),array('uniacid'=>$_SESSION['uniacid'],'id'=>$user['id']));
	}
	if(!$user['mid']){
		do{
			$username='user_'.random(10);
		}while(pdo_getcolumn('core_members',array('uniacid'=>$_SESSION['uniacid'],'username'=>$username),'id'));
		$member=array('username'=>$username,'password'=>trim($post['password']));
		$uid=model('core/member')->register($member);
		pdo_update($tablename,array('mid'=>$uid),array('uniacid'=>$_SESSION['uniacid'],'id'=>$user['id']));
		//exi('该机构没有绑定用户/管理员！','error');
	}
	$_SESSION['role']=2;
	$_SESSION['uid']=$user['mid']?:$uid;
	$_SESSION['userid']=$user['id'];
	$_SESSION['merchid']=$user['merchid'];
	$_SESSION['member']['username']=$user['name'];
	$url='jy_51job/merch.index/';
	if($_SESSION['os']=='windows'){
		exi('登陆成功!','',custom_url($url,true));
	}else{
		exi('登陆成功!','',$url);
	}
	
}